package cn.wyf.demo.controller;

import com.alibaba.fastjson.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import cn.wyf.demo.model.Person;
import cn.wyf.demo.model.PersonExample;
import cn.wyf.demo.service.PersonService;

@Controller
@RequestMapping("/person")
public class PersonController {

    public PersonService personService;

    @Autowired
    public void setPersonService(PersonService personService) {
        this.personService = personService;
    }

    @RequestMapping(value = "/getAllperson", method = RequestMethod.GET)
    public ResponseEntity<List<Person>> getAllPerson() {
        List<Person> personList = personService.getAllperson();
        return new ResponseEntity<List<Person>>(personList, HttpStatus.OK);
    }

    @RequestMapping(value = "/addPerson", method = RequestMethod.POST)
    @ResponseBody
    public String addPerson(HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        personService.addPerson(name, Integer.valueOf(age));
        return "{\"code\":\"200\",\"message\":\"success\"}";
    }

    @RequestMapping(value = "/addPerson2", method = RequestMethod.POST)
    @ResponseBody
    public String addPerson2(@RequestBody JSONObject s){
        System.out.println(s);
        String name = s.getString("name");
        String age = s.getString("age");
        personService.addPerson(name, Integer.valueOf(age));
        return "{\"code\":\"200\",\"message\":\"success\"}";
    }

    @RequestMapping(value = "/getperson",method = RequestMethod.GET)
    public @ResponseBody Person getPersnByid(HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        String id = request.getParameter("id");
        Person person = personService.getPersonByid(Integer.valueOf(id));
        return person;
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String updatePersonByid(HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        String name = request.getParameter("name");
        String id = request.getParameter("id");
        String age = request.getParameter("age");
        personService.updatePerson(new Person(Integer.valueOf(id),name,Integer.valueOf(age)));
        return "test";
    }

    @RequestMapping(value = "/testget",method = RequestMethod.POST)
    public @ResponseBody List<Person> getPerson(HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        PersonExample example = new PersonExample();
        PersonExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(request.getParameter("name"));
        criteria.andAgeEqualTo(Integer.valueOf(request.getParameter("age").trim()));
        List<Person> peoples = personService.testGetPerson(example);
        return peoples;
    }

    @RequestMapping(value = "/querList",method = RequestMethod.GET)
    public @ResponseBody List<Person> querList(HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        List<Person> peoples = personService.queryForList(new Person(Integer.valueOf(request.getParameter("id"))));
        return peoples;
    }
}